﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HHPS.CreditEducation.DataProvider;
using HHPS.CreditEducation.Frameworks.Commons;
using HHPS.CreditEducation.Frameworks.Constants;

namespace HHPS.CreditEducation.Presentation.StudyPlans
{
    public partial class CourseElementControl : UserControl
    {
        //public CourseElementControl()
        //{
        //    InitializeComponent();
        //}

        public StudyCourse StudyCourse;
        private readonly Business.Business _business;

        public CourseElementControl(StudyCourse course)
        {
            InitializeComponent();

            _business = MainView.Instance.CreditEducationBusiness;
            StudyCourse = course;

        }

        private void CourseElementControl_Load(object sender, EventArgs e)
        {
            Dock = DockStyle.Top;

            if (StudyCourse != null)
            {
                lblCourseName.Text = "Học kỳ: " + StudyCourse.Course.Name;
                chkSelected.DataBindings.Add("Checked", StudyCourse, "IsChecked");
                chkComplete.DataBindings.Add("Checked", StudyCourse, "IsComplete");
                dtpStartDate.Value = StudyCourse.StartDate;

                LoadStudyCourseDetail();
            }
        }

        private void LoadStudyCourseDetail()
        {
            dgvStudyCourseDetail.DataSource = _business.GetStudyCourseDetailByStudyCourse(StudyCourse.Id);
            DataGridViewHelper.ShowHideColumns(dgvStudyCourseDetail, new[]
                                                                   {
                                                                       (string) typeof (StudyCourseDetail).Properties().IsChecked,
                                                                       (string) typeof (StudyCourseDetail).Properties().SubjectName,
                                                                       (string) typeof (StudyCourseDetail).Properties().IsPass,
                                                                       (string) typeof (StudyCourseDetail).Properties().NumberOfCredit
                                                                   });
            DataGridViewHelper.ShowIsCheck(dgvStudyCourseDetail);

            dgvStudyCourseDetail.Columns[(string)typeof(StudyCourseDetail).Properties().IsChecked].HeaderText = string.Empty;
            dgvStudyCourseDetail.Columns[(string)typeof(StudyCourseDetail).Properties().IsPass].HeaderText = "Đã đạt";
            dgvStudyCourseDetail.Columns[(string)typeof(StudyCourseDetail).Properties().IsPass].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dgvStudyCourseDetail.Columns[(string)typeof(StudyCourseDetail).Properties().SubjectName].HeaderText = "Môn học";
            dgvStudyCourseDetail.Columns[(string)typeof(StudyCourseDetail).Properties().NumberOfCredit].HeaderText = "Số tín chỉ";

            var sum = 0;
            for (int i = 0; i < dgvStudyCourseDetail.RowCount; i++)
            {
                sum += Convert.ToInt32(dgvStudyCourseDetail[(string)typeof(StudyCourseDetail).Properties().NumberOfCredit, i].Value);
            }

            lblSumCredit.Text = string.Format("Tổng số tín chỉ: {0}", sum);
        }

        private void dtpStartDate_ValueChanged(object sender, EventArgs e)
        {
            StudyCourse.StartDate = dtpStartDate.Value;
            _business.SaveStudyCourse(StudyCourse);
        }

        private void chkComplete_CheckedChanged(object sender, EventArgs e)
        {
            StudyCourse.IsComplete = chkComplete.Checked;
            _business.SaveStudyCourse(StudyCourse);

            chkSelected.Enabled = !chkComplete.Checked;
            btnAddCourse.Enabled = !chkComplete.Checked;
            btnDeleteCourse.Enabled = !chkComplete.Checked;
            dtpStartDate.Enabled = !chkComplete.Checked;
            lblWaining.Visible = chkComplete.Checked;
        }

        #region Buttons Click

        private void btnDeleteCourse_Click(object sender, EventArgs e)
        {
            var studyCourseDetails = new List<StudyCourseDetail>();

            for (var i = 0; i < dgvStudyCourseDetail.RowCount; i++)
            {
                if (!(bool)dgvStudyCourseDetail[(string)typeof(StudyCourseDetail).Properties().IsChecked, i].Value) continue;

                studyCourseDetails.Add(new StudyCourseDetail
                {
                    Id = (int)dgvStudyCourseDetail[(string)typeof(StudyCourseDetail).Properties().Id, i].Value,

                });
            }

            if (studyCourseDetails.Count > 0)
            {
                if (MessageBox.Show(string.Format(ConstantMessages.ConfirmDeleteItem, " các môn học đã chọn"), FileConstants.PROGRAM_NAME, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    _business.DeleteStudyCourseDetails(studyCourseDetails);
                    LoadStudyCourseDetail();
                }
            }
        }

        private void btnAddCourse_Click(object sender, EventArgs e)
        {
            var selectSubject = new FormSelectSubject(StudyCourse);

            //Show and select new subject
            selectSubject.SelectedSubject += s =>
                                                 {
                                                     var studyCourseDetails = new List<StudyCourseDetail>();

                                                     foreach (var subject in s)
                                                     {
                                                         studyCourseDetails.Add(new StudyCourseDetail
                                                                                    {
                                                                                        IdStudyCourse = StudyCourse.Id,
                                                                                        IdSubject = subject.Id,
                                                                                        IsPass = false
                                                                                    });
                                                     }
                                                     _business.AddNewStudyCourseDetail(studyCourseDetails);

                                                     LoadStudyCourseDetail();
                                                 };

            selectSubject.Show();

        }

        #endregion

        private void dgvStudyCourseDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (chkComplete.Checked)
            {
                return;
            }

            if (dgvStudyCourseDetail != null && (
                (dgvStudyCourseDetail.Columns[(string)typeof(StudyCourseDetail).Properties().IsChecked] != null &&
                dgvStudyCourseDetail.Columns[(string)typeof(StudyCourseDetail).Properties().IsChecked].Index == e.ColumnIndex) ||
                (dgvStudyCourseDetail.Columns[(string)typeof(StudyCourseDetail).Properties().IsPass] != null &&
                dgvStudyCourseDetail.Columns[(string)typeof(StudyCourseDetail).Properties().IsPass].Index == e.ColumnIndex)))

                dgvStudyCourseDetail[e.ColumnIndex, e.RowIndex].Value =
                    !(bool)dgvStudyCourseDetail[e.ColumnIndex, e.RowIndex].Value;
        }
    }
}
